import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2022-09-28
 * Time: 22:42
 */
public class MyStack {

    //注意总要保持一个队列为空
    Queue<Integer> queue1;
    Queue<Integer> queue2;
    public MyStack() {
        queue1=new LinkedList<>();
        queue2=new LinkedList<>();
    }

    //那个不为空往哪个里面加
    public void push(int x) {
        if(queue1.isEmpty()){
            queue2.add(x);
        }else{
            queue1.add(x);
        }
    }

    public int pop() {
        if(queue1.isEmpty()){
            int size=queue2.size();
            //注意不能是i<queue2.size() 因为queue2.size()一直在变
            for (int i = 0; i < size-1; i++) {
                queue1.add(queue2.poll());
            }
            return queue2.poll();
        }else {
            int size=queue1.size();
            for (int i = 0; i < size-1; i++) {
                queue2.add(queue1.poll());
            }
            return queue1.poll();
        }

    }

    public int top() {
        int size=0;
        if(queue1.isEmpty()){
            size=queue2.size();
            for (int i = 0; i < size-1; i++) {
                queue1.add(queue2.poll());
            }
            int val=queue2.poll();
            queue1.add(val);
            return val;
        }else {
            size=queue1.size();
            for (int i = 0; i < size-1; i++) {
                queue2.add(queue1.poll());
            }
            int val=queue1.poll();
            queue2.add(val);
            return val;
        }
    }

    public boolean empty() {
        return queue1.isEmpty()&&queue2.isEmpty();
    }
}
